<?php

/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/11/5
 * Time: 3:24 PM
 */
class RsaUtil
{
     public $private_key = "-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDNOdKQvqGSoKZTuBcCFioTiEDUr/drQetnP5E1MedPK5FLL2td
Aczbtf3b2fT+TII6G+YguwjV8AhV0ftEayWPoPMo5Tby6ZHbWBQT2xYrPE3sIHEl
ANqdvH9XBMlxcze+OXBiIz3sO+rhN5f7V/XUb8+rbAPFpTpgWro6mb2A5wIDAQAB
AoGBAKzeXAQYz7jXIcO7eYYEbYTubM27O8pZSJU78nDAjHsf089lLia18X3RGvkp
6ziZ8chI39wKMDMlcZ/4tJK5PnTvbGaJb4mO9y0rI/hfvof5mZAfnFWaMPLS4vOc
JwRjSX4db+eZYCnH5AXpuqEydTb3pImFMntmfYnQd7YotucBAkEA68dtrJf6C7iA
iCN/++kutn7bj2lTN49pw2WHJ03gZQy2N+0woEOMpO3Ofqy7zR0PNCs6KulD8aja
SS5WksTshwJBAN7TmBlDTkZfE5vnSWD4h7K1syC7ADjkwAWKCu44eJP5f3gInUJQ
5EE1NrfzrLNPcVlEnGRia3mKqLWgj6TlQKECQC3y+6i7JXvLRSFC6dpKI1RlfJyC
Rjl5tX7i9mfugWCwDYLwsp1JDMvHnIqLVz4t/5yHIBrkwt0Bryi74xtPTVkCQQCb
4xogambjcnaMdmVuktCUZOuZdVn1c4snFOkKubUZiM31ksk58M+PEwRh8vaaR3S8
B/UFgZlA4ZGf52E28i+hAkBRdr7/LpGQg+N2xtELIcnBA/Yp3kZlwdNP9YllH5wl
QKiQxFRr/guzTvpM+ChuOVBmg1YIirKD2MStDCUrVT3W
-----END RSA PRIVATE KEY-----";
    public $public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNOdKQvqGSoKZTuBcCFioTiEDU
r/drQetnP5E1MedPK5FLL2tdAczbtf3b2fT+TII6G+YguwjV8AhV0ftEayWPoPMo
5Tby6ZHbWBQT2xYrPE3sIHElANqdvH9XBMlxcze+OXBiIz3sO+rhN5f7V/XUb8+r
bAPFpTpgWro6mb2A5wIDAQAB
-----END PUBLIC KEY-----";
    public function getPrivateKey(){
        return openssl_pkey_get_private($this->private_key);
    }
    public function getPublicKey(){
        return openssl_pkey_get_public($this->public_key);
    }
    /**
     * RSA签名
     * @param $data 待签名数据
     * @param $private_key_path 商户私钥文件路径
     * return 签名结果
     */
    function rsaSign($data, $private_key_path) {
        $priKey = file_get_contents($private_key_path);
        $res = openssl_get_privatekey($priKey);
        openssl_sign($data, $sign, $res);
        openssl_free_key($res);
        //base64编码
        $sign = base64_encode($sign);
        return $sign;
    }
    function rsaEncrypt($content, $public_key_path){
        $pubKey = file_get_contents($public_key_path);
        $res = openssl_get_publickey($pubKey);
        //把需要加密的内容，按128位拆开解密
        $result  = '';
        for($i = 0; $i < strlen($content)/128; $i++  ) {
            $data = substr($content, $i * 128, 128);
            openssl_public_encrypt ($data, $encrypt, $res);
            $result .= $encrypt;
        }
        $result = base64_encode($result);
        openssl_free_key($res);
        return $result;
    }
    /**
     * RSA解密
     * @param $content 需要解密的内容，密文
     * @param $private_key_path 商户私钥文件路径
     * return 解密后内容，明文
     */
    function rsaDecrypt($content) {

        $res = openssl_get_privatekey($this->private_key);
        //用base64将内容还原成二进制
        $content = base64_decode($content);
        //把需要解密的内容，按128位拆开解密
        $result  = '';
        for($i = 0; $i < strlen($content)/128; $i++  ) {
            $data = substr($content, $i * 128, 128);
            openssl_private_decrypt($data, $decrypt, $res);
            $result .= $decrypt;
        }
        openssl_free_key($res);
        return $result;
    }


}